package com.alongstudio.account.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alongstudio.account.action.base.BaseAction;
import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.constants.IViewConstant;
import com.alongstudio.account.constants.SystemConstant;
import com.alongstudio.account.entity.Account;
import com.alongstudio.account.entity.Category;
import com.alongstudio.account.entity.ClientUser;
import com.alongstudio.account.entity.KeepAccountsDetail;
import com.alongstudio.account.entity.Merchant;
import com.alongstudio.account.entity.Project;
import com.alongstudio.account.service.IKeepAccountsDetailService;
import com.alongstudio.account.vo.KeepAccountsDetailVo;

@SuppressWarnings("serial")
@Component("KeepAccountsAction")
@Scope("prototype")
public class KeepAccountsAction extends BaseAction {
	@Autowired
	private IKeepAccountsDetailService keepAccountService;
	private String className = KeepAccountsAction.class.getName();
	private List<Category> expenditureCategoryList;// 支出的类别
	private List<Category> subExpenditureCategoryList;// 支出的类别-子类
	private List<Category> incomeCategoryList;// 收入的类别
	private List<Category> subIncomeCategoryList;// 收入的类别-子类
	private List<Account> debtAccounts;// 负债账户
	private List<Account> loanOutAccounts;// 借出账户
	private List<Account> creditorAccounts;// 债权账户
	private List<Account> incomeAccounts;// 收入账户
	private List<Account> expenditureAccounts;// 支出账户
	private List<Account> allAccounts;// 所有账户
	private List<Project> projectList;
	private List<Merchant> merchantList;

	private KeepAccountsDetailVo detail;// 收支明细

	@Override
	public String executeAction() throws BusinessException {
		// 下拉框数据初始化
		try {
			Map map = keepAccountService.findKeepAccountsInitData(getCurrentClientUserId());
			expenditureCategoryList = (List<Category>) map.get("expenditureCategoryList");
			subExpenditureCategoryList = (List<Category>) map.get("subExpenditureCategoryList");

			incomeCategoryList = (List<Category>) map.get("incomeCategoryList");
			subIncomeCategoryList = (List<Category>) map.get("subIncomeCategoryList");

			allAccounts = (List<Account>) map.get("allAccounts");
			incomeAccounts = (List<Account>) map.get("incomeAccounts");
			expenditureAccounts = (List<Account>) map.get("expenditureAccounts");
			debtAccounts = (List<Account>) map.get("debtAccounts");
			loanOutAccounts = (List<Account>) map.get("loanOutAccounts");
			creditorAccounts = (List<Account>) map.get("creditorAccounts");

			projectList = (List<Project>) map.get("projectList");
			merchantList = (List<Merchant>) map.get("merchantList");
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	/**
	 * 新增收支明细
	 * 
	 * @return
	 * @throws BusinessException
	 */
	public String newExpenditure() throws BusinessException {
		boolean isOk = false;
		try {
			if (detail != null) {
				detail.setClientUserId(getCurrentClientUserId());

//				switch (detail.getType()) {
//				case SystemConstant.KEEP_ACCOUNT_TYPE_EXPENDITURE:// 0
//					detail.setToAccount(null);
//					if (detail.getMerchant().getId() == null) {
//						detail.setMerchant(null);
//					}
//					break;
//				case SystemConstant.KEEP_ACCOUNT_TYPE_INCOME:// 1
//					detail.setToAccount(null);
//					detail.setMerchant(null);
//					break;
//				case SystemConstant.KEEP_ACCOUNT_TYPE_TRANSFER:// 2
//				case SystemConstant.KEEP_ACCOUNT_TYPE_BORROW:// 3
//				case SystemConstant.KEEP_ACCOUNT_TYPE_LOAN_OUT:// 4
//				case SystemConstant.KEEP_ACCOUNT_TYPE_PAY_DEBT:// 5
//				case SystemConstant.KEEP_ACCOUNT_TYPE_COLLECT_DEBT:// 6
//					detail.setMerchant(null);
//					detail.setCategory(null);
//					break;
//				default:
//					break;
//				}
				detail.setType(SystemConstant.KEEP_ACCOUNT_TYPE_EXPENDITURE);
				detail.setCreateTime(new Date());
				isOk = keepAccountService.createDetail(detail);
				setOPERATOR_RESULT(isOk?IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		setOPERATOR_RESULT(isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED);
		return SUCCESS;
	}

	public List<Category> getExpenditureCategoryList() {
		return expenditureCategoryList;
	}

	public void setExpenditureCategoryList(List<Category> expenditureCategoryList) {
		this.expenditureCategoryList = expenditureCategoryList;
	}

	public List<Category> getSubExpenditureCategoryList() {
		return subExpenditureCategoryList;
	}

	public void setSubExpenditureCategoryList(List<Category> subExpenditureCategoryList) {
		this.subExpenditureCategoryList = subExpenditureCategoryList;
	}

	public List<Category> getIncomeCategoryList() {
		return incomeCategoryList;
	}

	public void setIncomeCategoryList(List<Category> incomeCategoryList) {
		this.incomeCategoryList = incomeCategoryList;
	}

	public List<Category> getSubIncomeCategoryList() {
		return subIncomeCategoryList;
	}

	public void setSubIncomeCategoryList(List<Category> subIncomeCategoryList) {
		this.subIncomeCategoryList = subIncomeCategoryList;
	}

	public List<Account> getDebtAccounts() {
		return debtAccounts;
	}

	public void setDebtAccounts(List<Account> debtAccounts) {
		this.debtAccounts = debtAccounts;
	}

	public List<Account> getIncomeAccounts() {
		return incomeAccounts;
	}

	public void setIncomeAccounts(List<Account> incomeAccounts) {
		this.incomeAccounts = incomeAccounts;
	}

	public List<Account> getExpenditureAccounts() {
		return expenditureAccounts;
	}

	public void setExpenditureAccounts(List<Account> expenditureAccounts) {
		this.expenditureAccounts = expenditureAccounts;
	}

	public List<Project> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}

	public List<Merchant> getMerchantList() {
		return merchantList;
	}

	public void setMerchantList(List<Merchant> merchantList) {
		this.merchantList = merchantList;
	}

	
	public List<Account> getAllAccounts() {
		return allAccounts;
	}

	public void setAllAccounts(List<Account> allAccounts) {
		this.allAccounts = allAccounts;
	}

	public List<Account> getLoanOutAccounts() {
		return loanOutAccounts;
	}

	public void setLoanOutAccounts(List<Account> loanOutAccounts) {
		this.loanOutAccounts = loanOutAccounts;
	}

	public List<Account> getCreditorAccounts() {
		return creditorAccounts;
	}

	public void setCreditorAccounts(List<Account> creditorAccounts) {
		this.creditorAccounts = creditorAccounts;
	}

	public KeepAccountsDetailVo getDetail() {
		return detail;
	}

	public void setDetail(KeepAccountsDetailVo detail) {
		this.detail = detail;
	}

}
